package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.domain.Callback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

@Slf4j
public abstract class SendChatMessagesHandler implements CallbackHandler {

    @Override
    public boolean ignoreError() {
        return false;
    }

    @Override
    public void handle(Callback callback) {
        String serialNo = callback.getSerialNo();
        JSONObject data = (JSONObject) callback.getData();
        String relaSerialNo = data.getString("vcRelaSerialNo");
        if (!StringUtils.hasText(relaSerialNo)) {
            log.warn("未知的消息发送类型[id:{}, serialNo:{}, relaSerialNo:{}]", callback.getId(), serialNo, relaSerialNo);
            return;
        }

        String[] split = relaSerialNo.split(":");
        if (split.length != 2) {
            log.warn("未知的消息发送类型[id:{}, serialNo:{}, relaSerialNo:{}]", callback.getId(), serialNo, relaSerialNo);
            return;
        }
        String actionType = split[0];
        String actionId = split[1];

        handle0(actionType, actionId, callback, data);
    }

    protected abstract void handle0(String actionType, String actionId, Callback callback, JSONObject data);
}
